package com.xuanyang.dormrepair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xuanyang.dormrepair.config.hook.AdminLogin;
import com.xuanyang.dormrepair.config.hook.AppLogin;
import com.xuanyang.dormrepair.config.hook.OnceLogin;
import com.xuanyang.dormrepair.model.entity.FloorRoom;
import com.xuanyang.dormrepair.model.entity.Repairs;
import com.xuanyang.dormrepair.model.enums.RepairStatus;
import com.xuanyang.dormrepair.service.RepairService;
import com.xuanyang.dormrepair.utils.Const;
import com.xuanyang.dormrepair.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @AUTHOR soft
 * @DATE 2019/5/22 21:46
 * @DESCRIBE 报修相关接口
 */
@RestController
@RequestMapping("/api/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;

    private FloorRoom room;

    @ModelAttribute
    public void init(@RequestAttribute(value = Const.USER, required = false) FloorRoom rm) {
        this.room = rm;
    }

    @OnceLogin
    @PostMapping("/new")
    public RestResponse news() {
        int news = repairService.news();
        return RestResponse.ok(news);
    }

    /**
     * 维修报告
     */
    @OnceLogin
    @PostMapping("/report")
    public RestResponse report(Repairs repairs) {
        repairs.setStatus(RepairStatus.COMMENT)
                .setReportTime(new Date());
        if (repairService.updateId(repairs)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 分配维修工
     * @param repairs 包含id和维修工id
     */
    @AdminLogin
    @PostMapping("/allot")
    public RestResponse allot(Repairs repairs) {
        repairs.setStatus(RepairStatus.DOING);
        if (repairService.updateId(repairs)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @OnceLogin
    @PostMapping("/page")
    public RestResponse page(int page, int limit, Repairs query) {
        IPage<Repairs> iPage = repairService.page(page, limit, query);
        if (iPage.getTotal() > 0) {
            return RestResponse.ok(iPage);
        }
        return RestResponse.fail();
    }

    @OnceLogin
    @PostMapping("/{id}")
    public RestResponse one(@PathVariable Integer id) {
        Repairs repairs = repairService.byId(id);
        if (repairs != null) {
            return RestResponse.ok(repairs);
        }
        return RestResponse.fail();
    }

    ////////////////////////////

    @AppLogin
    @PostMapping("/self")
    public RestResponse self(int page, int limit, Repairs query) {
        query.setFloorId(room.getId());
        return this.page(page, limit, query);
    }

    @AppLogin
    @PostMapping("/add")
    public RestResponse add(Repairs repairs) {
        repairs.setFloorId(room.getId())
                .setHouseNo(room.getHouseNo())
                .setRoomNo(room.getRoomNo())
                .setStatus(RepairStatus.WAITING);
        if (repairService.add(repairs)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @AppLogin
    @PostMapping("/cancel/{id}")
    public RestResponse cancel(@PathVariable Integer id) {
        if (repairService.update(new Repairs().setStatus(RepairStatus.CANCEL),
                new Repairs().setId(id).setFloorId(room.getId()).setStatus(RepairStatus.WAITING))) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @AppLogin
    @PostMapping("/comment")
    public RestResponse comment(Repairs repairs) {
        repairs.setCommentTime(new Date())
                .setCommentGrade(5)
                .setStatus(RepairStatus.FINISH);
        if (repairService.updateId(repairs)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @AppLogin
    @PostMapping("/change")
    public RestResponse change(Repairs repairs) {
        if (repairService.update(repairs, new Repairs().setId(repairs.getId()).setFloorId(room.getId()))) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
